<?php

namespace plugin\cms\app\controller\admin;

use app\bll\ComponentElement as BllComponentElement;
use Exception;
use plugin\cms\app\bll\Model as BllModel;
use support\View;
use app\bll\Datadict as BllDatadict;
use core\controller\AdminBase;
use plugin\cms\app\bll\ModelField as BllModelField;

class Model extends AdminBase
{

    public function index()
    {
        $bllModel = new BllModel();
        $models   = $bllModel->select(siteid: [0, request()->nowsiteid]);
        View::assign('models', $models);
        return $this->view();
    }

    public function add()
    {
        if (is_post()) {
            try {
                $bllModel       = new BllModel();
                $data           = request()->post();
                $data['siteid'] = request()->nowsiteid;

                $bllModel->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    public function edit()
    {
        $bllModel = new BllModel();
        $modelid  = request()->input('modelid');
        if (is_post()) {
            try {
                $bllModel->edit($modelid,request()->nowsiteid, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $modelinfo = $bllModel->find($modelid,request()->nowsiteid);
            View::assign('modelinfo', $modelinfo);
            return  $this->view();
        }
    }

    /**
     * 删除模型
     *
     * @return void
     */
    public function delete()
    {
        try {
            $bllModel =  new BllModel();
            $bllModel->delete(request()->input('modelid'), request()->nowsiteid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    /**
     * 字段管理
     *
     * @return void
     */
    public  function fieldmanage()
    {
        $bllModelField = new BllModelField();
        $bllModel = new BllModel();
        $modelinfo = $bllModel->find(request()->get('modelid'),request()->nowsiteid);
        if (!$modelinfo) {
            throw new Exception('模型不存在');
        }
        if (is_post()) {
            try {
                $bllModelField->batchsave(
                    array_values(request()->post('field')),
                    $modelinfo
                );
            } catch (Exception $ex) {
                return $this->error($ex);
            }
            return $this->success();
        } else {

            $fields = $bllModelField->select(request()->get('modelid'));

            //获取表单元素
            $bllComponentElement = new BllComponentElement();
            $elements = $bllComponentElement->select();


            $bllDatadict = new BllDatadict();
            $dicts = $bllDatadict->select();

            View::assign('dicts', array_column($dicts, null, 'id'));
            View::assign('elements', array_column($elements, null, 'ident'));
            View::assign('fields', $fields);

            return $this->view(frame: false);
        }
    }

    public  function fields()
    {
        $bllModelField = new BllModelField();
        $fields = $bllModelField->select(request()->get('modelid'));
        View::assign('fields', $fields);
        return $this->view();
    }

    public function addfield()
    {
        if (is_post()) {
            try {
                $data          = request()->post('field');
                $bllModelField = new BllModelField();
                $bllModelField->batchAdd(array_values($data), request()->get('modelid'));
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            //获取表单元素
            $bllComponentElement = new BllComponentElement();
            $elements = $bllComponentElement->select();


            $bllDatadict = new BllDatadict();
            $dicts = $bllDatadict->select();

            View::assign('dicts', $dicts);

            View::assign('elements', $elements);
            return $this->view(frame: false);
        }
    }

    public function editfield()
    {
        $bllModelField = new BllModelField();
        if (is_post()) {
        } else {
            $fieldinfo =  $bllModelField->find(request()->get('fieldid'));

            //获取表单元素
            $bllComponentElement = new BllComponentElement();
            $elements = $bllComponentElement->select();

            $bllDatadict = new BllDatadict();
            $dicts = $bllDatadict->select();

            View::assign('dicts', $dicts);
            View::assign('elements', $elements);
            View::assign('fieldinfo', $fieldinfo);
            return $this->view(frame: false);
        }
    }
}
